﻿Imports System.Runtime.Serialization
Imports System.Security.Cryptography

Public Class User
    Private cro, cwo, cra, cwa, crc, cwc, crd, cwd, cu As Boolean
    Private _name, _id As String
    Private _loginfailed As Boolean = True

    Public Sub New(name As String, password As String, db As IDatabase)
        Dim up As nHibernate.Models.UserPreviewModel
        Try
            up = db.GetUser(name)
        Catch ex As Exception
            _loginfailed = True
            _name = ""
            _id = ""
            Log.AddError(ex.ToString)
            Exit Sub
        End Try

        Dim input As String = name + password + (up.CanReadOrder.ToString + up.CanWriteOrder.ToString + up.CanReadArticle.ToString + up.CanWriteArticle.ToString + up.CanReadCustomer.ToString + up.CanWriteCustomer.ToString +
                                                 up.CanReadDatabase.ToString + up.CanWriteDatabase.ToString + up.CanEditUsers.ToString).ToLower
        Dim hash = GetHash(input)
        If hash = up.Hash Then
            cro = up.CanReadOrder
            cwo = up.CanWriteOrder
            cra = up.CanReadArticle
            cwa = up.CanWriteArticle
            crc = up.CanReadCustomer
            cwc = up.CanWriteCustomer
            crd = up.CanReadDatabase
            cwd = up.CanWriteDatabase
            cu = up.CanEditUsers
            _name = up.UserName
            _id = up.UserID
            _loginfailed = False
        Else
            _loginfailed = True
        End If
    End Sub

    Public ReadOnly Property LogInFailed As Boolean
        Get
            Return _loginfailed
        End Get
    End Property

    Private Function GetHash(input As String) As String
        Dim dataToMAC As Byte() = System.Text.Encoding.UTF8.GetBytes(input)
        Using hmacsha1 As New HMACSHA256(System.Text.Encoding.UTF8.GetBytes("EBFD359A91C23FEC2E3267D5FAE87A37F7156C0B0524CBDE533225F10605C913"))
            Return System.Convert.ToBase64String(hmacsha1.ComputeHash(dataToMAC))
        End Using
    End Function

    Public ReadOnly Property CanReadOrder As Boolean
        Get
            Return cro
        End Get
    End Property
    Public ReadOnly Property CanWriteOrder As Boolean
        Get
            Return cwo
        End Get
    End Property

    Public ReadOnly Property CanReadArticle As Boolean
        Get
            Return cra
        End Get
    End Property
    Public ReadOnly Property CanWriteArticle As Boolean
        Get
            Return cwa
        End Get
    End Property

    Public ReadOnly Property CanReadCustomer As Boolean
        Get
            Return crc
        End Get
    End Property
    Public ReadOnly Property CanWriteCustomer As Boolean
        Get
            Return cwc
        End Get
    End Property

    Public ReadOnly Property CanReadDatabase As Boolean
        Get
            Return crd
        End Get
    End Property
    Public ReadOnly Property CanWriteDatabase As Boolean
        Get
            Return cwd
        End Get
    End Property

    Public ReadOnly Property CanEditUsers As Boolean
        Get
            Return cu
        End Get
    End Property

    Public ReadOnly Property Name As String
        Get
            Return _name
        End Get
    End Property
    Public ReadOnly Property ID As String
        Get
            Return _id
        End Get
    End Property

    Public Shared Function DatabaseWriteDenied() As String
        Return "Schreibzugriff Datenbank"
    End Function
    Public Shared Function DatabaseReadDenied() As String
        Return "Lesezugriff Datenbank"
    End Function

    Public Shared Function OrderWriteDenied() As String
        Return "Schreibrecht für Auftragsdaten"
    End Function
    Public Shared Function OrderReadDenied() As String
        Return "Leserecht für Auftragsdaten"
    End Function

    Public Shared Function ArticleWriteDenied() As String
        Return "Schreibrecht für Artikeldaten"
    End Function
    Public Shared Function ArticleReadDenied() As String
        Return "Leserecht für Artikeldaten"
    End Function

    Public Shared Function CustomerWriteDenied() As String
        Return "Schreibrecht für Kundendaten"
    End Function
    Public Shared Function CustomerReadDenied() As String
        Return "Leserecht für Kundendaten"
    End Function
End Class